package agents;

import extra.Position;
import game.constants.ReferenceAgent;
import game.constants.ReferenceSpecialFeature;
import interfaces.impl.Agent;
import objects.SpetialFeature;
import objects.WorldObject;
import universe.Universe;
import actions.ActionIncreaseLife;
import actions.ActionObjectFinish;
import constants.Constants;



public class AgentBuild extends Agent{
	
	double contTime=0;


	
	SpetialFeature isBuilding;
	SpetialFeature increaseLife;
	SpetialFeature objectIdToBuild;
	SpetialFeature attackRange;
	
	ActionObjectFinish actionObjectFinish;
	
	@Override
	public void initializeActionList() {
		
		actionObjectFinish= new ActionObjectFinish();
		addAction(actionObjectFinish);
	}

	@Override
	public void initializeSpecialFeatures() {
		isBuilding = new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_IS_BUILDING);
		addSpetialFeature(isBuilding);
		
		increaseLife = new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_LIFE_INCREASE_PER_SECOND);
		addSpetialFeature(increaseLife);
		
		objectIdToBuild = new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_OBJECT_ID_TO_BUILD);
		addSpetialFeature(objectIdToBuild);
		
		attackRange= new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_ATTACK_RANGE);
		addSpetialFeature(attackRange);
		
	}

	@Override
	public void initializeName() {
		name="agent Build";		
	}

	@Override
	public void initializeId() {
		id=ReferenceAgent.AGENT_BUILD;
		
	}

	@Override
	public void updateChild(WorldObject object, Universe universe, double time) {
		contTime+=time;
		if(contTime<1000){
			return;
		}
		contTime=0;
		if(isBuilding.getValue()==Constants.FALSE){
			return;
		}
		System.out.println("incremento la vida de lo que contrsutoadsasdashdka1");
		
		WorldObject objToBuild = universe.getObject((int)objectIdToBuild.getValue());
		if(objToBuild==null){return;}
		
		System.out.println("incremento la vida de lo que contrsutoadsasdashdka2");
		
		SpetialFeature objIsBuilding = objToBuild.getFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_IS_BUILDING);
		if(objIsBuilding.getValue()==Constants.FALSE){
			System.out.println("ya acabe");
			ActionObjectFinish actionObjectFinish = new ActionObjectFinish();
			actionObjectFinish.setSource(object);
			actionObjectFinish.setTarget(object);
			universe.setAction(actionObjectFinish);
			return;
		}
			
		System.out.println("incremento la vida de lo que contrsutoadsasdashdka3");
		
		SpetialFeature currentLife = objToBuild.getFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_CURRENT_LIFE);
		SpetialFeature life = objToBuild.getFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_LIFE);
		if(life==null ||currentLife==null ){return;}
		
		if(currentLife.getValue()>=life.getValue()){
			
			return;
		}
		
		System.out.println("incremento la vida de lo que contrsutoadsasdashdka2");
		
		double distance = (Position.DistanceBetween(object.getPosition(), objToBuild.getPosition()));
		
		if(distance>attackRange.getValue()){
			return;
		}
		
		System.out.println("incremento la vida de lo que contrsutoadsasdashdka3");
		
		ActionIncreaseLife actionIncreaseLife = new ActionIncreaseLife();
		actionIncreaseLife.setSource(object);
		actionIncreaseLife.setTarget(objToBuild);
		actionIncreaseLife.setLifeToIncrease(increaseLife.getValue());
		universe.setAction(actionIncreaseLife);
		
		
		
	}
	
	

}
